# 直线

def count_lines(x, y):
    # 构建所有的点
    point_list = []
    for i in range(x):
        for j in range(y):
            point_list.append((i, j))
    # print(point_list)
    n = len(point_list)

    # 遍历所有的点来构建所有的直线
    line_set = set()
    for i in range(n):
        for j in range(i, n):
            x1, y1 = point_list[i]
            x2, y2 = point_list[j]
            if x1 == x2:
                line_set.add(("INF", x1))
                continue
            k = (y2 - y1) / (x2 - x1)
            # b = y1 - k * x1  这里是错误的主要原因, 因为k是小数可能会失去精度, 造成相同直线的bias可能会不同, 也就是会多统计的原因
            """直线的两点式方程, 也就是直线上任意一点的斜率与两点计算的斜率相同
            (y - y1) / (x - x1) = (y2 - y1) / (x2 - x1) -> (y - y1) / (y2 - y1) = (x - x1) / (x2 - x1)
            当y = 0时, x的值即为bias:
            -y1 / (y2 - y1) = (x - x1) / (x2 - x1)
            x = (x1 * y2 - x2 * y1) / y2 - y1
            直线的斜截式方程, 其中a为x截距, b为y截距.
            x / a + y / b = 1 -> bx + ay = ab
            从几何角度很好理解: bx为左侧矩形的面积 ay为下侧矩形的面积(下侧矩形的面积)
            重复的区域的面积=xy 空白的区域=(a-x)(b-y) 由于x/a = y/b 即证明 xy = (a-x)(b-y)
            |\------|
            | \  |  |
            |  \ |  |
            |   \|  |
            |----\--|
            |    |\ |
            |____|_\|
            """
            b = (x2 * y1 - x1 * y2) / (x2 - x1)
            line_set.add((k, b))
    return len(line_set)


if __name__ == '__main__':
    print(count_lines(1, 2))
    print(count_lines(3, 3))
    print(count_lines(2, 3))
    print(count_lines(3, 4))
    print(count_lines(4, 5))
    print(count_lines(20, 21))
